package group9;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

/**
 * @author Claudio Herreros, Dylan James, Tyler Berg
 *
 */

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User implements Serializable{

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String fullName;
	
	@Persistent
	private String userName;

	@Persistent
	private String password;
	
	@Persistent(mappedBy = "creator")
	private List<UserStory> myUserStories;
	
	@Persistent
	private List<Key> estUserStories;
	
	@Persistent
	private List<Boolean> estimated;
	
	private static String specialChars = "-+_%@*&";
	
	public User(String fullName, String userName, String password)
	{
		this.fullName = fullName;
		this.userName = userName;
		this.password = password;
		this.myUserStories = new ArrayList<UserStory>();
		this.estUserStories = new LinkedList<Key>();
		this.estimated = new LinkedList<Boolean>();
	}
	
	public String getFullName()
	{
		return fullName;
	}
	
	public String getUserName()
	{
		return userName;
	}
	
	public Key getKey()
	{
		return this.key;
	}
	
	public List<UserStory> getMyUserStories()
	{
		return myUserStories;
	}
	
	public List<Boolean> getEstimated()
	{
		return this.estimated;
	}
	
	public List<Key> getEstUserStories()
	{
		return this.estUserStories;
	}
	
	public boolean checkPassword(String password)
	{
		return this.password.equals(password);
	}
	
	public void setPassword(String password)
	{
		this.password = password;
	}
	
	public void addUserStory(UserStory userStory)
	{
		myUserStories.add(userStory);
	}
	// This method doesn't run correctly, gets to a null pointer exception
	public void addEstUserStory(Key usKey)
	{
		//System.out.println(usKey.ge);
		//System.out.println("Userhere1");
		//System.out.println(userStory.getKey() + " " + userStory.getKey());
		//this.estUserStories.add(usKey);
		//System.out.println("Userhere2");
		//this.estimated.add(false);
		//System.out.println("Userhere3");

	}
	
	public boolean estimate(Key key)
	{
		int index = estUserStories.indexOf(key);
		
		if (index == -1)
			return false;
		
		estimated.set(index, true);
		return true;
	}
	
	public static boolean isLegalPassword(String password)
	{
		if (password.length() < 8)
			return false;
		
		boolean num = false;
		boolean letter = false;
		boolean special = false;
		
		for (int i = 0; i < password.length(); i++)
		{
			if (Character.isLetter(password.charAt(i)))
				letter = true;

			else
			{
				if (Character.isDigit(password.charAt(i)))
					num = true;
				else
				{
					for (int j = 0; j < specialChars.length(); j++)
					{
						if (specialChars.charAt(j) == password.charAt(i))
						{
							special = true;
							break;
						}
					}
				}	
			}
		}
		
		return num && letter && special;
	}
}
